rm(list= ls())

library(tidyverse)
library(MatchIt)
library(fixest)

## Define balance function 

get_bal <- function (treatvar, 
                     cov_list, data.df, FE = NULL, 
                     weights = NULL) {
  out_temp <- lapply(cov_list, function(cv) {
    if (length(FE) == 0) {
      f <- as.formula(paste0(cv, " ~", treatvar))
    }
    else {
      f <- as.formula(paste0(cv, " ~", treatvar, "+", FE))
    }
    data.df[, cov_list] <- scale(data.df[, cov_list])
    if (!is.null(weights)) {
      m <- lm(f, data = data.df, weight = data.df %>% pull(!!weights))
    }
    else {
      m <- lm(f, data = data.df)
    }
    coef_list <- summary(m)$coefficients[2, 1]
    lower_list <- coef_list - 1.96 * summary(m)$coefficients[2, 
                                                             2]
    upper_list <- coef_list + 1.96 * summary(m)$coefficients[2, 
                                                             2]
    data.frame(cov = cv, lower = lower_list, upper = upper_list, 
               coef = coef_list, tv = treatvar, stringsAsFactors = F)
  })
  do.call("rbind", out_temp)
}

## County level election data (94-17)

county_elec <- read_rds('data/elections_federal_county_94_17.rds') %>% 
  filter(date > 2008) %>% 
  mutate(left_total_party  = greens + spd + left) %>% #
  mutate(right_total_party  = afd + cdu_csu) %>% 
  dplyr::select(ags, left_total_party, right_total_party, date, turnout) %>% 
  filter(!substr(ags, 1, 2) == '13') %>%    ## Exclude MV
  mutate(year = lubridate::year(date))

## County-level covars (observed in 2015, cross-section)

ccovars <- read_rds('data/countycovs_germany_2015.rds') %>% 
  dplyr::rename(ags = 1) %>% 
  dplyr::select(-year)

## Merge

match_dat <- county_elec %>% 
  filter(year == 2013) %>% 
  left_join(ccovars)

## Exclude treated units in NRW and BW 
## I.e. only use untreated units from these two states 

match_dat <- match_dat %>% 
  filter(!(treated == 1 & state_id %in% c('05', '09')))

## Reverse treatment, exclude East

match_dat <- match_dat %>%
  mutate(treated_reversed = ifelse(treated == 0, 1, 0)) %>%
  filter(state_id <= 10) 

## Original data (used for balance plot) 

match_dat_orig <- match_dat %>%
  mutate(treated_reversed = ifelse(treated == 0, 1, 0))

## Declare vars to match on 

matchvars <- c('left_total_party',
               'unemp_rate_nat',
               'foreign_share')

## Exclude NAs 

match_dat <- match_dat[complete.cases(match_dat[ ,matchvars]),]

## 1 to 1 matching w/o replacement

match_formula <- as.formula(paste('treated_reversed ~ ', 
                                  paste0(matchvars, collapse = '+')))

out <- MatchIt::matchit(match_formula, 
                        data = match_dat, 
                        method = 'optimal', 
                        distance = 'mahalanobis',
                        ratio = 1,
                        replace = F) 

## Add weights

match_dat$weight <- out$weights

## Exclude unmatched obs.

match_dat_matched <- match_dat %>% 
  filter(weight > 0)

## Create complete DF 

county_elec <- county_elec %>% 
  filter(year >= 2013) %>%                       ## Only 2013 and later 
  filter(ags %in% match_dat_matched$ags)  %>%    ## Only matched AGS  
  left_join(ccovars)                             ## Merge county-level covars

## Define treatment and post indicators 

county_elec <- county_elec %>%                       
  mutate(treated_reversed = ifelse(treated == 0, 1, 0),
         post = 1*(year == 2017),
         treated_reversed_post = post*treated_reversed,
         treated_post = treated*post)

## Vote share on 0-100 scale

county_elec <- county_elec %>% 
  mutate(left_total_party = left_total_party*100,
         right_total_party = right_total_party*100)

#### Table A.11: Matching results #### 

m1 <- feols(fml = c(right_total_party, left_total_party) ~ 
              treated_reversed + post + treated_reversed_post,
            data = county_elec,
            cluster = 'AA.district.id') %>%
  etable(.,
         tex = T, fitstat = ~r2 + n,
         keep = 'treated_reversed_post',
         dict=c(right_total_party="Total right vote, excl. FDP",
                left_total_party="Total left vote"),
         digits = 3, title = 'Matching results')

m1

#### Figure A.9: Balance before and after matching ####

get_bal('treated_reversed', matchvars, match_dat_matched) %>%
  mutate(what = 'Matched data set') %>% 
  bind_rows(get_bal('treated_reversed', matchvars, match_dat_orig) %>% 
              mutate(what = 'Original data set')) %>% 
  mutate(cov = dplyr::recode(cov, 
                             `unemp_rate_nat` = 'Unemployment rate',
                             `left_total_party` = 'Total left vote (2013)',
                             `foreign_share` = 'Share of foreigners')) %>% 
  ggplot(aes(cov, coef)) +
  geom_hline(yintercept = 0) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0) +
  geom_point(shape= 21, fill = 'white', size = 2) +
  theme_bw() +
  coord_flip() +
  ylab('Standardized difference') +
  xlab('') +
  facet_wrap(~what)

